#pragma once
#include <vector>
#include "BasePlayer.h"
#include "Params.h"
#include "StateMachine.h"
#include "Goal.h"

class FieldPlayer;
class GoalKeeper;


class Team
{
public:

	Team(GameState* game, Params::TeamColor color);
	~Team(void);
	
	void					PrepareForKickOff(bool myTurn);
	bool					ReadyForKickOff();
	void					KickOff();
	void					SendPlayersToHome();

	//GETTERS
	GameState* const 		Game()const{ return m_game; }
	Goal*	const			HomeGoal()const{ return m_homeGoal; }
	float					DistBallToHomeGoal(){ return m_distBallToHomeGoal; }
	Goal*	const			OpponentGoal()const{ return m_oppGoal; }
	Team*	const			OpponentTeam()const{ return (m_color == Params::blue) ? m_game->GetRedTeam(): m_game->GetBlueTeam(); }
	StateMachine<Team>*		GetFSM()const{ return m_FSM; }
	std::vector<BasePlayer*>Players()const{ return m_players; }
	BasePlayer* const		TeamPlayerClosestToBall()const{ return m_TPlayerClosestBall; }
	BasePlayer* const		TeamSecondPlayerClosestToBall()const{ return m_TSecondPlayerClosestBall; }
	BasePlayer* const		TeamPlayerClosestToHomeGoal()const{ return m_TPlayerClosestToHomeGoal; }
	BasePlayer* const		TeamPlayerClosestToOppGoal()const{ return m_TPlayerClosestToOppGoal; }
	BasePlayer* const		ControllingPlayer()const{ return m_TPlayerController; }
	BasePlayer* const		Gk()const{ return (*m_players.begin()); }
	bool					InControl()const{ return ( m_TPlayerController == NULL) ? false : true ;}
	Params::TeamColor		Color()const{ return m_color;}

	void					GkControllBall(){m_game->GetRedTeam()->GkHasBall(true); m_game->GetBlueTeam()->GkHasBall(true);}
	void					GkLostBall(){m_game->GetRedTeam()->GkHasBall(false); m_game->GetBlueTeam()->GkHasBall(false);}
	bool					IsGkWithBall(){ return m_gkWithBall;}

	inline bool				IsUserControlled() { return m_userControlled; }
	inline bool				IsKeyboardControlled() { return m_keyboardControlled; }
	inline bool				IsGamepadControlled() { return m_gamepadControlled; }
	void					SetPlayerUserControlled(BasePlayer* bp);
	inline BasePlayer*		GetPlayerUserControlled(){return m_playerUserControlled;}

	//SETTERS
	void					SetControllingPlayer(BasePlayer* newController){ m_TPlayerController = newController;}		
	void					LostControl(){ m_TPlayerController = NULL;}
	
	void					ChangeRegions(const int* regions);
	void					Update(const sf::Input& input);
	void					OnDraw(sf::RenderWindow& window);
	void					DrawOnRadar(sf::RenderWindow& window,const sf::Vector2f& offset);

	inline void				SetUserControl(bool controlled) { m_userControlled = controlled; }
	inline void				SetKeyboardControl(bool kControlled) { m_keyboardControlled = kControlled; }
	inline void				SetGamepadControl(bool gControlled) { m_gamepadControlled = gControlled; }
	
private:

	GameState*						m_game;
	Goal*							m_homeGoal;
	Goal*							m_oppGoal;
	
	Params::TeamColor				m_color;
	std::vector<BasePlayer*>		m_players;
	StateMachine<Team>*				m_FSM;
	
	BasePlayer*						m_TPlayerClosestBall;			//Team's player closest to ball.
	BasePlayer*						m_TSecondPlayerClosestBall;		//Team's second player closest to ball.
	BasePlayer*						m_TPlayerClosestToHomeGoal;		//Team's player closest to home goal.
	BasePlayer*						m_TPlayerClosestToOppGoal;		//Team's player closest to opp goal.
	BasePlayer*						m_TPlayerController;			//Team's player with the ball.

	bool							m_gkWithBall;

	float							m_distBallToHomeGoal;

	void							CreatePlayers();
	void							GkHasBall(bool b){ m_gkWithBall = b; }

	bool							m_userControlled;
	bool							m_keyboardControlled;
	bool							m_gamepadControlled;

	BasePlayer*						m_playerUserControlled;

};
